#include <Ecore_Evas.h>
#include <Ecore.h>
#include <Edje.h>

#define WIDTH 480
#define HEIGHT 640

#include "main.h"
#include "gui.h"

typedef struct _gui_struct gui_proto;

struct _gui_struct {
  Ecore_Evas*  ee;
  Evas*        evas;
  Evas_Object* edje;
  Ecore_Timer* timer;
};

gui_proto gui;

static const char *gui_theme_pretends[]={
  "./" NAME ".edj",
  "~/." NAME ".edj",
  "~/." NAME "/theme.edj",
  "/usr/share/" NAME "/theme.edj",
  NULL
};

static const char *
gui_get_theme(){
  int i;
  for(i=0; gui_theme_pretends[i]; i++){
    printf("Try %s\n", gui_theme_pretends[i]);
    if(edje_file_group_exists(gui_theme_pretends[i], "main"))
      return gui_theme_pretends[i];
  }
  return NULL;
}

static int
gui_update_data(void* d){
  static char real_aver[16];
  static char real_freq[16];
  static char note_freq[16];
  static char note_octa[8];
  static char delta[8];
  
  static double real_aver_value;
  static double real_freq_value;
  static double note_freq_value;
  static const char* note_name;
  static unsigned char note_octa_value;
  static double delta_value;
  
  if(main_produce(&real_aver_value, &real_freq_value, &note_freq_value, &note_name, &note_octa_value, &delta_value)){
    sprintf(real_freq, "%.3f", real_freq_value);
    sprintf(note_freq, "%.3f", note_freq_value);
    sprintf(delta, "%.0f", delta_value);
    sprintf(note_octa, "%d", note_octa_value);
    
    //if(edje_object_part_exists(gui.edje, "real.freq value"))
    edje_object_part_text_set(gui.edje, "real.freq value", real_freq);
    //if(edje_object_part_exists(gui.edje, "note.freq value"))
    edje_object_part_text_set(gui.edje, "note.freq value", note_freq);
    //if(edje_object_part_exists(gui.edje, "note.name value"))
    edje_object_part_text_set(gui.edje, "note.name value", note_name);
    //if(edje_object_part_exists(gui.edje, "note.octa value"))
    edje_object_part_text_set(gui.edje, "note.octa value", note_octa);
    
    //if(edje_object_part_exists(gui.edje, "delta value")) 
    edje_object_part_text_set(gui.edje, "delta value", delta);
    
    printf("Updated!\n");
  }
  
  sprintf(real_aver, "%.3f", real_aver_value);
  //if(edje_object_part_exists(gui.edje, "volume level value"))
  edje_object_part_text_set(gui.edje, "volume level value", real_aver);
  
  edje_object_signal_emit(gui.edje, "update", "");
  
  if(gui.timer){
    ecore_timer_del(gui.timer);
    gui.timer=ecore_timer_add(0.1, gui_update_data, NULL);
  }
  return 0;
}

static void
gui_activate(void *data, Evas_Object *obj, const char *emission, const char *source){
  if(gui.timer==NULL){
    main_activate();
    
    gui.timer=ecore_timer_add(0.1, gui_update_data, NULL);
    
    printf("activate..\n");
  }
}

static void
gui_deactive(void *data, Evas_Object *obj, const char *emission, const char *source){
  if(gui.timer){
    ecore_timer_del(gui.timer);
    gui.timer=NULL;
    
    main_deactive();
    
    edje_object_part_text_set(gui.edje, "volume level value", "0.0");
    edje_object_signal_emit(gui.edje, "update", "");
    
    printf("deactive..\n");
  }
}

static void
gui_threshold_changed(void *data, Evas_Object *obj, const char *emission, const char *source){
  main_threshold=atof(edje_object_part_text_get(obj, source));
  printf("%f\n", main_threshold);
}

/* this resizes the edje to fit the window */
static void
gui_resize_window(Ecore_Evas *ee) {
  Evas_Coord w, h;
  /* check how big the window is currently */
  ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
  /* set the evas-object (edje) to the window size */
  evas_object_resize(gui.edje, w, h);
}

static int
gui_signal_exit(void *data, int type, void *event){
  gui_deactive(NULL, gui.edje, NULL, NULL);
  ecore_main_loop_quit();
  return 1;
}

int
gui_main(void){
  const char* theme;
  
  ecore_init();
  ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT, gui_signal_exit, NULL);
  ecore_evas_init();
  gui.ee = ecore_evas_software_x11_new(NULL, 0, 0, 0, WIDTH, HEIGHT);
  ecore_evas_callback_resize_set(gui.ee, gui_resize_window);
  ecore_evas_title_set(gui.ee, NAME);
  ecore_evas_borderless_set(gui.ee, 0);
  ecore_evas_shaped_set(gui.ee, 0);
  
  ecore_evas_show(gui.ee);
  gui.evas = ecore_evas_get(gui.ee);
  edje_init();
  gui.edje = edje_object_add(gui.evas);
  
  theme = gui_get_theme();
  
  if(!edje_object_file_set(gui.edje, theme, "main")){
    printf("Couldn't load the edje theme. It should be %s\n", theme);
    return 1;
  }
  /*
  {
    unsigned int i;
    Eina_List* cl=edje_file_collection_list(theme);
    for(i=0;i<eina_list_count(cl);i++) printf("G:%s\n", eina_list_nth(cl,i));
    edje_file_collection_list_free(cl);
  }
  {
    unsigned int i;
    Eina_List* pl=edje_edit_parts_list_get(gui.edje);
    for(i=0;i<eina_list_count(pl);i++) printf("P:%s=%d\n", eina_list_nth(pl,i), edje_object_part_exists(gui.edje, eina_list_nth(pl,i)));
    edje_edit_string_list_free(pl);
  }
  */
  {
    Evas_Coord   edje_w, edje_h;
    
    evas_object_move(gui.edje, 0, 0);
    edje_object_size_min_get(gui.edje, &edje_w, &edje_h);
    evas_object_resize(gui.edje, edje_w, edje_h);
    evas_object_show(gui.edje);
    ecore_evas_resize(gui.ee, (int)edje_w, (int)edje_h);
    ecore_evas_show(gui.ee);
  }
  /* Insert Objects and callbacks here */
  
  edje_object_signal_callback_add(gui.edje, "volume threshold changed", "volume threshold value", gui_threshold_changed, NULL);
  edje_object_signal_callback_add(gui.edje, "activate", "power button", gui_activate, NULL);
  edje_object_signal_callback_add(gui.edje, "deactive", "power button", gui_deactive, NULL);
  
  {
    char txt[6];
    snprintf(txt, 6, "%.3f", main_threshold);
    edje_object_part_text_set(gui.edje, "volume threshold value", txt);
    edje_object_signal_emit(gui.edje, "load", "");
  }
  
  ecore_main_loop_begin();
  
  edje_shutdown();
  evas_shutdown();
  ecore_shutdown();
  return 0;
}
